package com.example.telesys.controller;

import com.example.telesys.pojo.ApiResponse;
import com.example.telesys.pojo.Supplier;
import com.example.telesys.service.SupplierService;
import com.example.telesys.test.SendTransactionMessage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/supplier")
public class SupplierController {

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @Autowired
    private SendTransactionMessage sendTransactionMessage;

    @RequestMapping("/selectSupplierById")
    public Supplier selectSupplierById(@RequestParam("supplierid") String supplierid) {
        return supplierService.selectSupplierById(supplierid);
    }

    @RequestMapping("/addPhonePriceTotal")
    public int addPhonePriceTotal(
            @RequestParam("phoneTotal") Integer phoneTotal,
            @RequestParam("supplierid") String supplierid) {
        return supplierService.addPhonePriceTotal(phoneTotal, supplierid);
    }

    @RequestMapping("/addBroadPriceTotal")
    public int addBroadPriceTotal(
            @RequestParam("broadbandTotal") Integer broadbandTotal,
            @RequestParam("supplierid") String supplierid) {
        return supplierService.addBroadPriceTotal(broadbandTotal, supplierid);
    }

    @RequestMapping("/addTrafficPriceTotal")
    public int addTrafficPriceTotal(
            @RequestParam("trafficTotal") Integer trafficTotal,
            @RequestParam("supplierid") String supplierid) {
        return supplierService.addTrafficPriceTotal(trafficTotal, supplierid);
    }


    @RequestMapping("/selectSupplierData")
    public PageInfo selectSupplierData(@RequestParam("pageNumber") Integer pageNumber,
                                       @RequestParam("pageSize") Integer pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<Supplier> list = supplierService.selectSupplierData();
        Supplier supplier = list.get(0);
        /**
         * RocketMQ demo当查看供应商数据后 向MQ队列发送消息
         * 并在相应地方 订阅与接收这个消息
         */
        //参数1 指定topic 参数2 指定消息体
        rocketMQTemplate.convertAndSend("supplier-topic", supplier);
        PageInfo pageInfo = new PageInfo(list, pageSize);
        return pageInfo;
    }

    @RequestMapping("/addSupplier")
    public ApiResponse addSupplier(@RequestBody Supplier supplier) {
        TransactionSendResult transactionSendResult = sendTransactionMessage.createSupplierBefore(supplier);
        return new ApiResponse(200, "添加成功", transactionSendResult);
    }

}
